#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <set>

#define flr(x, l, r) for (int x = l; x <= r; ++x)
#define frl(x, r, l) for (int x = r; x >= l; --x)
#define fmp(x, t) for (int x = h[t]; ~x; x = ne[x])
#define LL long long
#define mt memset
#define my memcpy
#define szf sizeof
#define INF 0x3f3f3f3f
#define in(x) scanf("%d", &x)
#define out(x) printf("%d", x)
#define inll(x) scanf("%lld", &x)
#define outll(x) printf("%lld", x)
#define pn printf("\n")
#define con continue
#define bk break
#define vc vector
#define pb push_back
#define sz size
#define PII pair<int, int>
#define x first
#define y second
#define P_q priority_queue
#define ft front
#define pf push_front
#define popf pop_front
#define it insert
#define ct count

using namespace std;

const int N = 500010;

int a[N], b[N];

LL merge(int l, int r) {
    if (l >= r) return 0;
    
    int mid = l + r >> 1;
    LL res = merge(l, mid) + merge(mid + 1, r);
    int i = l, j = mid + 1, k = 0;
    
    while (i <= mid && j <= r) {
        if (a[i] <= a[j]) b[k ++ ] = a[i ++ ];
        else {
            res += mid - i + 1;
            b[k ++ ] = a[j ++ ];
        }
    }
    
    while (i <= mid) b[k ++ ] = a[i ++ ];
    while (j <= r) b[k ++ ] = a[j ++ ];
    int hh = 0;
    flr (i, l, r) a[i] = b[hh ++ ];
    
    return res;
}

int main() {
    int n;
    
    while (in(n), n) {
        flr (i, 0, n - 1) in(a[i]);
        
        outll(merge(0, n - 1)), pn;
    }

    return 0;
}